﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace TruyenTranhCrawler.Helper
{
    public class ImageHelper
    {
        /// <summary>
        /// 0: google url full,
        /// 1: googleImageFolder
        /// 2: GoogleImageFileName
        /// 3: Width
        /// 4: Height
        /// </summary>
        public static string[] GetImage(string urlWebFile, string fileName, string virtualFolder)
        {
            var results=new string[5];  //get out values when download and upload to picasa
            try
            {
                //prepare
                if (!virtualFolder.EndsWith("/"))
                    virtualFolder += "/";
                virtualFolder += DateTime.Now.ToString("yyyyMMdd") + "/";

                if (!Directory.Exists(Config.PhysicalStaticFile + virtualFolder))
                    Directory.CreateDirectory(Config.PhysicalStaticFile + virtualFolder);

                System.Net.WebRequest req = System.Net.WebRequest.Create(urlWebFile);

                //get fileName and folderPath
                string fName = virtualFolder + fileName + ".jpg";

                Stream str = req.GetResponse().GetResponseStream();

                var fs = new FileStream(Config.PhysicalStaticFile + fName, FileMode.Create);

                byte[] buf = ReadFully(str);

                fs.Write(buf, 0, buf.Length);

                fs.Close();
                //upload to google
                //upload to picasa
                var bloggerAcc = GoogleHelper.GetBloggerAccount();
                try
                {
                    var gInfos = GoogleHelper.UploadImageToGoogle(Config.PhysicalStaticFile + fName, fileName, bloggerAcc.UserName, bloggerAcc.Password, "truyengicungco");
                    if (gInfos.Count > 0)
                    {
                        var googleImage = gInfos["url"].ToString();
                        var idx = googleImage.LastIndexOf('/');
                        results[0] = googleImage;
                        results[1] = googleImage.Substring(0, idx + 1);
                        results[2] = googleImage.Substring(idx + 1);
                        results[3] = gInfos["width"].ToString();
                        results[4] = gInfos["height"].ToString();
                        //remove image temps here...
                        FileHelper.Remove(fName);
                    }
                }
                catch { }
            }
            catch
            {
            }
            return results;
        }
        private static byte[] ReadFully(Stream stream)
        {
            var buffer = new byte[32768];
            using (var ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
    }
}
